package wjh.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import wjh.mapper.DoctorInfoMapper;
import wjh.pojo.Doctor;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class DoctorInfoImpl implements DoctorInfoService{
    @Autowired
    private DoctorInfoMapper doctorInfoMapper;

    @Override
    public Doctor findDoctorById(int id) {
        return doctorInfoMapper.findDoctorById(id);
    }

    @Override
    public void updateInfo(Doctor doctor, MultipartFile file, HttpServletRequest request) {
        String photo="a0.jpg";
        if (file.getSize()>0){
            //获取文件的原始名称 a.jog  bbb.gif
            String originalFilename = file.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            String prefix = UUID.randomUUID().toString().replaceAll("-","");
            photo=prefix+suffix;
            String realPath = request.getServletContext().getRealPath("/static/img");
            //执行上传
            try {
                file.transferTo(new File(realPath,photo));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        doctor.setPhoto(photo);
        doctorInfoMapper.updateInfo(doctor);
    }

    @Override
    public Doctor findDoctorLogin(String phone, String password) {
        return doctorInfoMapper.findDoctorLogin(phone,password);
    }

    @Override
    public Doctor findDoctorId(String phone, String password) {
        return doctorInfoMapper.findDoctorId(phone,password);
    }

    @Override
    public Doctor findDoctorRegister(String phone) {
        return doctorInfoMapper.findDoctorRegister(phone);
    }

    @Override
    public void register(String phone, String password) {
        doctorInfoMapper.register(phone,password);
    }
}
